package com.mall4j.cloud.product.controller.platform;

import com.mall4j.cloud.api.product.vo.BrandVO;
import com.mall4j.cloud.api.product.vo.SpuVO;
import com.mall4j.cloud.common.database.dto.PageDTO;
import com.mall4j.cloud.common.database.vo.PageVO;
import com.mall4j.cloud.common.response.ServerResponseEntity;
import com.mall4j.cloud.common.security.AuthUserContext;
import com.mall4j.cloud.product.dto.AttrDTO;
import com.mall4j.cloud.product.dto.BrandDTO;
import com.mall4j.cloud.product.dto.SpuDTO;
import com.mall4j.cloud.product.model.*;
import com.mall4j.cloud.product.service.BrandService;
import com.mall4j.cloud.product.service.CategoryBrandService;
import com.mall4j.cloud.product.service.SpuService;
import io.swagger.annotations.ApiOperation;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 品牌
 *
 * @author JCM
 * @date 2021-04-24 21:02:00
 */
@RestController("platformBrandController")
@RequestMapping("/platform/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @Autowired
    private CategoryBrandService categoryBrandService;

    @Autowired
    private SpuService spuService;

    @Autowired
    private MapperFacade mapperFacade;

    @GetMapping("/page")
    @ApiOperation(value = "分页获取品牌列表", notes = "分页获取品牌列表")
    public ServerResponseEntity<PageVO<BrandVO>> page(@Valid PageDTO pageDTO, BrandDTO brandDTO) {
        PageVO<BrandVO> brandPage = brandService.page(pageDTO, brandDTO);
        return ServerResponseEntity.success(brandPage);
    }

    @GetMapping
    @ApiOperation(value = "获取品牌", notes = "根据brandId获取品牌")
    public ServerResponseEntity<BrandVO> getByAttrId(@RequestParam Long brandId) {
        return ServerResponseEntity.success(brandService.getByBrandId(brandId));
    }

    @Transactional
    @PostMapping
    @ApiOperation(value = "保存属性", notes = "保存属性")
    public ServerResponseEntity<Void> save(@Valid @RequestBody BrandDTO brandDTO) {
        Brand brand = mapperFacade.map(brandDTO, Brand.class);
        if(brandDTO.getCategoryIds().isEmpty()){
            return ServerResponseEntity.showFailMsg("请选择分类");
        }
        brandService.save(brand);

        List<Long> categoryIds = brandDTO.getCategoryIds();
        for (Long categoryId : categoryIds){
            CategoryBrand categoryBrand = new CategoryBrand();
            categoryBrand.setBrandId(brand.getBrandId());
            categoryBrand.setCategoryId(categoryId);
            categoryBrandService.save(categoryBrand);
        }

        return ServerResponseEntity.success();
    }

    @PutMapping("/update_brand_status")
    @ApiOperation(value = "下架品牌", notes = "下架品牌")
    public ServerResponseEntity<Boolean> updateStatus(@Valid @RequestBody BrandDTO brandDTO) {
        BrandVO brandVO = brandService.getByBrandId(brandDTO.getBrandId());
        Brand brand = mapperFacade.map(brandVO, Brand.class);
        brand.setStatus(brandDTO.getStatus());
        brandService.update(brand);
        return ServerResponseEntity.success();
    }

    @Transactional
    @PutMapping
    @ApiOperation(value = "更新属性", notes = "更新属性")
    public ServerResponseEntity<Void> update(@Valid @RequestBody BrandDTO brandDTO) {
        if(brandDTO.getCategoryIds().size() == 0){
            return ServerResponseEntity.showFailMsg("请选择分类");
        }
        Brand brand = mapperFacade.map(brandDTO, Brand.class);
        Long brandId = brandDTO.getBrandId();
        //设置原始状态
        BrandVO brandVO = brandService.getByBrandId(brandId);
        brand.setStatus(brandVO.getStatus());
        brandService.update(brand);

        //直接删除原来与品牌关联的分类，保存新的
        categoryBrandService.deleteByBrandId(brandId);
        List<Long> categoryIds = brandDTO.getCategoryIds();
        for (Long categoryId : categoryIds){
            CategoryBrand categoryBrand = new CategoryBrand();
            categoryBrand.setBrandId(brandId);
            categoryBrand.setCategoryId(categoryId);
            categoryBrandService.save(categoryBrand);
        }

        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @ApiOperation(value = "删除品牌", notes = "根据品牌id删除品牌")
    public ServerResponseEntity<Void> delete(@RequestParam Long brandId) {
        SpuDTO spuDTO = new SpuDTO();
        spuDTO.setBrandId(brandId);
        List<SpuVO> list = spuService.list(spuDTO);
        if(list.size() != 0){
            return ServerResponseEntity.showFailMsg("该品牌正在被使用，无法删除");
        }
        brandService.deleteById(brandId);
        return ServerResponseEntity.success();
    }
}
